﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class AuthorityService
    {
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int InsertUser(UserInfo User)
        {
            string sql = "insert into UserInfoes(UserName,DisplayName,Email)";
            sql += " values(@UserName,@DisplayName,@Email)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",User.UserName),
                new SqlParameter("@DisplayName",User.DisplayName),
                new SqlParameter("@Email",User.Email)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="UserAuthority"></param>
        /// <returns></returns>
        public int InsertUserTeamRoles(UserTeamRole UserAuthority)
        {
            string sql = "insert into UserTeamRoles(UserName,TeamId,RoleId,WorkMouldId)";
            sql += " values(@UserName,@TeamId,@RoleId,@WorkMouldId)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",UserAuthority.UserName),
                new SqlParameter("@TeamId",UserAuthority.TeamId),
                new SqlParameter("@RoleId",UserAuthority.RoleId),
                new SqlParameter("@WorkMouldId",UserAuthority.WorkMouldId)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 获取用户用户团队角色工作流号对应信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<UserTeamRole> GetUserTeamRoles(UserTeamRole user)
        {
            string sql = "select * from UserTeamRoles";
            sql += " inner join UserInfoes on UserTeamRoles.UserName=UserInfoes.UserName";
            sql += " inner join Roles on UserTeamRoles.RoleId=Roles.RoleId";
            sql += " inner join Teams on UserTeamRoles.TeamId=Teams.TeamId";
            sql += " inner join WorkflowMoulds on UserTeamRoles.WorkMouldId=WorkflowMoulds.WorkParentId";
            sql += " where 1=1";
            List<SqlParameter> param = new List<SqlParameter>();
            if(user.WorkMouldId!=null)
            {
                sql += " and WorkMouldId=@WorkMouldId";
                param.Add(new SqlParameter("@WorkMouldId", user.WorkMouldId));
            }
            if(!string.IsNullOrEmpty(user.UserName))
            {
                sql += " and UserName=@UserName";
                param.Add(new SqlParameter("@UserName", user.UserName));
            }
            List<UserTeamRole> list = new List<UserTeamRole>();
            SqlDataReader objReader = SQLHelper.GetReader(sql, param.ToArray());
            while(objReader.Read())
            {
                list.Add(new UserTeamRole()
                    {
                        Displayname = objReader["DisplayName"].ToString(),
                        TeamName = objReader["TeamName"].ToString(),
                        RoleName = objReader["RoleName"].ToString(),
                        WorkName = objReader["WorkName"].ToString()
                    });
            }
            objReader.Close();
            return list;
        }
    }
}
